package com.vf.admin.utils.douyin;

import com.alibaba.fastjson.JSON;
import com.vf.admin.utils.douyin.entity.*;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *抖音话题主页获取无水印视频 
 */
public class HuaTiZhuYeUtils {
    private String charset = "utf-8";  
    private HttpClientUtil httpClientUtil = null;  
      
    public HuaTiZhuYeUtils(){  
        httpClientUtil = new HttpClientUtil();  
    }  
      
    public String test(String url){  
        String httpOrgCreateTest = url;
        System.out.println(httpOrgCreateTest);
        Map<String,String> createMap = new HashMap<String,String>();
//        createMap.put("Accept-Encoding","gzip"); 
        createMap.put("Host","api.amemv.com"); 
        createMap.put("sdk-version","1"); 
        createMap.put("Connection","Keep-Alive"); 
        createMap.put("Cookie","odin_tt=ba4d9c6f74f6410bfda880d4d1e79bd1e1013b879ddb8b972e31298ba04266474449628037c44dc831c7f04984c998a8f2da4dfd3cb76e973d937884ae922369; qh[360]=1; install_id=64634128482; ttreq=1$d4f3b99e5065f5068f5d3558028d32c3b887f8a9");
        createMap.put("X-Gorgon","03006cc044002f8c84f38d0933e0a9cd4c78470890c00f03b7af"); 
        createMap.put("X-Khronos","1551441293");
        createMap.put("User-Agent","okhttp/3.10.0.1");
        createMap.put("X-Pods","");
        
        
        
//        Accept-Encoding: gzip
//        sdk-version: 1
//        Cookie: odin_tt=ba4d9c6f74f6410bfda880d4d1e79bd1e1013b879ddb8b972e31298ba04266474449628037c44dc831c7f04984c998a8f2da4dfd3cb76e973d937884ae922369; qh[360]=1; install_id=64634128482; ttreq=1$d4f3b99e5065f5068f5d3558028d32c3b887f8a9
//        X-Gorgon: 0326560d4400c6880283ad79226c0271797dc857242aa3973f8b
//        X-Khronos: 1551437537
//        X-Pods: 
//        Host: api.amemv.com
//        Connection: Keep-Alive
//        User-Agent: okhttp/3.10.0.1
        
        
        
        String httpOrgCreateTestRtn = httpClientUtil.doPostHuaTi(httpOrgCreateTest,createMap,charset);  
        
        System.out.println(httpOrgCreateTestRtn);  
        return httpOrgCreateTestRtn;
    }  
      
//    public static void main(String[] args){  
//    	YinYueZhuYeUtils zhuYeMain = new YinYueZhuYeUtils();
//    	for(int i=0;i<10000;i++){
//    		int e = 20;
////    		zhuYeMain.request(i*e);
//    		System.out.println("--------------------------------------------------------");
//    	}
//    	
//    }  
    
    /**
     * 请求列表
     */
    public List<FanHui> convert(String huatiid,int count){
    	try{
//    		String url = "https://api.amemv.com/aweme/v1/challenge/aweme/?ch_id="+huatiid+"&query_type=0&cursor="+count+"&count=20&type=5&retry_type=no_retry&mcc_mnc=46007&iid=64634128482&device_id=66505446575&ac=wifi&channel=aweGW&aid=1128&app_name=aweme&version_code=510&version_name=5.1.0&device_platform=android&ssmix=a&device_type=SM-G955N&device_brand=samsung&language=zh&os_api=19&os_version=4.4.2&uuid=354730010701393&openudid=708bcd4f96671361&manifest_version_code=510&resolution=720*1280&dpi=240&update_version_code=5102&_rticket=1551434951590&ts=1551434951&as=a1953067673c0c54892200&cp=07c5c65c7c9f7843e1mkuo&mas=01a89214d5f6daef3229e50b16c8db6da10c0c4c4c9c1c2cacc60c";
//    		String url = "https://api.amemv.com/aweme/v1/challenge/aweme/?ch_id=1608399116661828&query_type=0&cursor=0&count=20&type=5&retry_type=no_retry&mcc_mnc=46007&iid=64634128482&device_id=66505446575&ac=wifi&channel=aweGW&aid=1128&app_name=aweme&version_code=510&version_name=5.1.0&device_platform=android&ssmix=a&device_type=SM-G955N&device_brand=samsung&language=zh&os_api=19&os_version=4.4.2&uuid=354730010701393&openudid=708bcd4f96671361&manifest_version_code=510&resolution=720*1280&dpi=240&update_version_code=5102&_rticket=1551434951590&ts=1551434951&as=a1953067673c0c54892200&cp=07c5c65c7c9f7843e1mkuo&mas=01a89214d5f6daef3229e50b16c8db6da10c0c4c4c9c1c2cacc60c";
    		String url = "https://api.amemv.com/aweme/v1/challenge/detail/?ch_id=1608399116661828&query_type=0&click_reason=0&retry_type=no_retry&mcc_mnc=46007&iid=64634128482&device_id=66505446575&ac=wifi&channel=aweGW&aid=1128&app_name=aweme&version_code=510&version_name=5.1.0&device_platform=android&ssmix=a&device_type=SM-G955N&device_brand=samsung&language=zh&os_api=19&os_version=4.4.2&uuid=354730010701393&openudid=708bcd4f96671361&manifest_version_code=510&resolution=720*1280&dpi=240&update_version_code=5102&_rticket=1551438403368&ts=1551438402&as=a165318742c42cb2e92400&cp=124ec4572d967d22e1ukco&mas=01c6912c4fde13dc261e46e8f969a4c0e50c0c2c4c9ca64c46c64c";
    		List<FanHui> fanHuiList = new ArrayList<FanHui>();
        	HuaTiZhuYeUtils main = new HuaTiZhuYeUtils();  
            String result = main.test(url);  
            DouYinShiPin douyinShiPin = JSON.parseObject(result,DouYinShiPin.class);
            List<Aweme> awemeList = douyinShiPin.getAweme_list();
            String palyurlStr = "";
            String gequming = "";
            if(awemeList!=null && awemeList.size()>0){
            	for(Aweme aweme : awemeList){
            		 String ickname =  aweme.getAuthor().getNickname();
                     String signature = aweme.getAuthor().getSignature();
                     String uid = aweme.getAuthor().getUid();
                      String verify = aweme.getAuthor().getCustom_verify();
                      String userid = aweme.getAuthor_user_id();
                      String miaoshu = aweme.getDesc();
                      Music music = aweme.getMusic();
                      if(music!=null){
                    	  gequming = aweme.getMusic().getTitle();
                    	  PlayUrl palyurl = music.getPlay_url();
                    	  if(palyurl!=null){
                    		  List urlist = palyurl.getUrl_list();
                    		  if(urlist!=null && urlist.size()>0){
                    			  palyurlStr = urlist.get(0)+"";
                    		  }
                    	  }
                      }
                      Video video = aweme.getVideo();
                      String vediourl = "";
                      if(video!=null){
                    	  PlayAddres payaddress = video.getPlay_addr();
                    	  if(payaddress!=null){
                    		  List playUrlList = payaddress.getUrl_list();
                              if(playUrlList!=null && playUrlList.size()>0){
                            	  vediourl = playUrlList.get(0).toString();
                              }
                    	  }
                      }
                      System.out.println("ickname:"+ickname+"  signature:"+signature+"   uid:"+uid+"   verify:"+verify+"   userid:"+userid+"  palyurlStr:"+palyurlStr);
                      FanHui fanhui = new FanHui();
                      fanhui.setZuozhe(filterEmoji(ickname));
                      fanhui.setMiaoshu(filterEmoji(miaoshu));
                      fanhui.setDownloadurl(vediourl);
                      fanhui.setGequming(filterEmoji(gequming));
                      fanHuiList.add(fanhui);
            	}
            }
            return fanHuiList;
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return null;
    }
    
    public static void main(String[] args){
    	String url = "https://api.amemv.com/aweme/v1/aweme/post/?max_cursor=0&user_id=58047835859&count=20&retry_type=no_retry&mcc_mnc=46007&iid=64634128482&device_id=66505446575&ac=wifi&channel=aweGW&aid=1128&app_name=aweme&version_code=510&version_name=5.1.0&device_platform=android&ssmix=a&device_type=SM-G955N&device_brand=samsung&language=zh&os_api=19&os_version=4.4.2&uuid=354730010701393&openudid=708bcd4f96671361&manifest_version_code=510&resolution=720*1280&dpi=240&update_version_code=5102&_rticket=1551441293936&ts=1551441293&as=a1a581874d289c8d992033&cp=118ec35ddf9870d4e1skao&mas=0157525795952bf49da86cf36c2e2d10a4cccc0c4c9c9c261cc69c";
		List<FanHui> fanHuiList = new ArrayList<FanHui>();
    	HuaTiZhuYeUtils main = new HuaTiZhuYeUtils();  
        String result = main.test(url);  
    }
    
    
    public String tiaoZhuanUrl(String url) {  
        try {  
            System.out.println("访问地址:" + url);  
            URL serverUrl = new URL(url);  
            HttpURLConnection conn = (HttpURLConnection) serverUrl  
                    .openConnection();  
            conn.setRequestMethod("GET");  
            // 必须设置false，否则会自动redirect到Location的地址  
            conn.setInstanceFollowRedirects(false);  
  
            conn.addRequestProperty("Accept-Charset", "UTF-8;");  
            conn.addRequestProperty("User-Agent",  
                    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");  
            conn.addRequestProperty("Referer", "http://zuidaima.com/");  
            conn.connect();  
            String location = conn.getHeaderField("Location");  
  
            serverUrl = new URL(location);  
            conn = (HttpURLConnection) serverUrl.openConnection();  
            conn.setRequestMethod("GET");  
  
            conn.addRequestProperty("Accept-Charset", "UTF-8;");  
            conn.addRequestProperty("User-Agent",  
                    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");  
            conn.addRequestProperty("Referer", "http://zuidaima.com/");  
            conn.connect();  
            System.out.println("跳转地址:" + location);  
            return location;
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;
    }  
    
    /**
     * 将emoji表情替换成空串
     *  
     * @param source
     * @return 过滤后的字符串
     */ 
    public String filterEmoji(String source) { 
     if (source != null && source.length() > 0) { 
      return source.replaceAll("[\ud800\udc00-\udbff\udfff\ud800-\udfff]", ""); 
     } else { 
      return source; 
     } 
    }
    
}